﻿using Leeder.Logger;
using LeederGlobal;
using SPSAndong.Business;
using SPSAndong.Comm;
using SPSAndong.Handle;
using SPSAndong.Model;
using Sunny.UI;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SPSAndong.Forms
{
    public partial class FrmSystemSet : UIPage
    {
        /// <summary>
        /// 音乐设置集合
        /// </summary>
        List<MusicSetModel> musicSetList;
        /// <summary>
        /// 广播设置集合
        /// </summary>
        List<BroadcastSetModel> broadcastSetList;
        /// <summary>
        /// 
        /// </summary>
        MusicSetBLL musicSetBLL = new MusicSetBLL();
        /// <summary>
        /// 
        /// </summary>
        BroadcastSetBLL broadcastSetBLL = new BroadcastSetBLL();
        /// <summary>
        /// 
        /// </summary>
        ProjectSetBLL projectSetBLL = new ProjectSetBLL();
        /// <summary>
        /// 
        /// </summary>
        SystemSetupBLL systemSetupBLL = new SystemSetupBLL();
        /// <summary>
        /// 判断窗体是否首次加载
        /// </summary>
        bool isLoad = true;

        /// <summary>
        /// 软件运行时实时选中的列表框
        /// </summary>
        UIDataGridView dataGridView;

        SPSAndongHandler sPSAndongHandler = SPSAndongHandler.getInstance();

        /// <summary>
        /// 日志
        /// </summary>
        public LogOperate LogObj => Log.GetLogger(this.GetType());

        public FrmSystemSet()
        {
            InitializeComponent();
            intMusicNo_T_IP.Location = new Point(82, 44);
            intMusicNo_T_IP.Size = new Size(100, 16);
            intMusicNo_C_EG.Location = new Point(82, 44);
            intMusicNo_C_EG.Size = new Size(100, 16);
            intMusicNo_T_F1.Location = new Point(82, 44);
            intMusicNo_T_F1.Size = new Size(100, 16);
            intMusicNo_T_F2.Location = new Point(82, 44);
            intMusicNo_T_F2.Size = new Size(100, 16);
            intMusicNo_DR.Location = new Point(82, 44);
            intMusicNo_DR.Size = new Size(100, 16);

            swMusicNF_T_IP.Location = new Point(82, 44);
            swMusicNF_T_IP.Size = new Size(100, 16);
            swMusicNF_C_EG.Location = new Point(82, 44);
            swMusicNF_C_EG.Size = new Size(100, 16);
            swMusicNF_T_F1.Location = new Point(82, 44);
            swMusicNF_T_F1.Size = new Size(100, 16);
            swMusicNF_T_F2.Location = new Point(82, 44);
            swMusicNF_T_F2.Size = new Size(100, 16);
            swMusicNF_DR.Location = new Point(82, 44);
            swMusicNF_DR.Size = new Size(100, 16);

            intMusictime_T_IP.Location = new Point(82, 44);
            intMusictime_T_IP.Size = new Size(100, 16);
            intMusictime_C_EG.Location = new Point(82, 44);
            intMusictime_C_EG.Size = new Size(100, 16);
            intMusictime_T_F1.Location = new Point(82, 44);
            intMusictime_T_F1.Size = new Size(100, 16);
            intMusictime_T_F2.Location = new Point(82, 44);
            intMusictime_T_F2.Size = new Size(100, 16);
            intMusictime_DR.Location = new Point(82, 44);
            intMusictime_DR.Size = new Size(100, 16);

            swMusicIsPlayAlong_T_IP.Location = new Point(82, 44);
            swMusicIsPlayAlong_T_IP.Size = new Size(100, 16);
            swMusicIsPlayAlong_C_EG.Location = new Point(82, 44);
            swMusicIsPlayAlong_C_EG.Size = new Size(100, 16);
            swMusicIsPlayAlong_T_F1.Location = new Point(82, 44);
            swMusicIsPlayAlong_T_F1.Size = new Size(100, 16);
            swMusicIsPlayAlong_T_F2.Location = new Point(82, 44);
            swMusicIsPlayAlong_T_F2.Size = new Size(100, 16);
            swMusicIsPlayAlong_DR.Location = new Point(82, 44);
            swMusicIsPlayAlong_DR.Size = new Size(100, 16);

            swBroadcastNF1.Location = new Point(82, 18);
            swBroadcastNF1.Size = new Size(100, 16);
            swBroadcastNF2.Location = new Point(82, 18);
            swBroadcastNF2.Size = new Size(100, 16);
            swBroadcastNF3.Location = new Point(82, 18);
            swBroadcastNF3.Size = new Size(100, 16);
            swBroadcastNF4.Location = new Point(82, 18);
            swBroadcastNF4.Size = new Size(100, 16);
            swBroadcastNF5.Location = new Point(82, 18);
            swBroadcastNF5.Size = new Size(100, 16);
            swBroadcastNF6.Location = new Point(82, 18);
            swBroadcastNF6.Size = new Size(100, 16);
            swBroadcastNF7.Location = new Point(82, 18);
            swBroadcastNF7.Size = new Size(100, 16);
            swBroadcastNF8.Location = new Point(82, 18);
            swBroadcastNF8.Size = new Size(100, 16);
            swBroadcastNF9.Location = new Point(82, 18);
            swBroadcastNF9.Size = new Size(100, 16);
        }

        private void FrmSystemSet_Load(object sender, EventArgs e)
        {
            if (!isLoad)
                return;
            isLoad = false;

            dgvT_IP.AutoGenerateColumns = false;
            dgvT_IP.AllowUserToAddRows = false;
            dgvT_IP.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvT_IP.ReadOnly = true;
            dgvT_IP.AddColumn("序号", "ID", 10);
            dgvT_IP.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvT_IP.AddColumn("岗位工程名称", "WorkName", 35);
            dgvT_IP.AddColumn("信号源", "SignalMark", 20);           

            dgvC_EG.AutoGenerateColumns = false;
            dgvC_EG.AllowUserToAddRows = false;
            dgvC_EG.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvC_EG.ReadOnly = true;
            dgvC_EG.AddColumn("序号", "ID", 10);
            dgvC_EG.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvC_EG.AddColumn("岗位工程名称", "WorkName", 35);
            dgvC_EG.AddColumn("信号源", "SignalMark", 20);

            dgvT_F1.AutoGenerateColumns = false;
            dgvT_F1.AllowUserToAddRows = false;
            dgvT_F1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvT_F1.ReadOnly = true;
            dgvT_F1.AddColumn("序号", "ID", 10);
            dgvT_F1.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvT_F1.AddColumn("岗位工程名称", "WorkName", 35);
            dgvT_F1.AddColumn("信号源", "SignalMark", 20);

            dgvT_F2.AutoGenerateColumns = false;
            dgvT_F2.AllowUserToAddRows = false;
            dgvT_F2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvT_F2.ReadOnly = true;
            dgvT_F2.AddColumn("序号", "ID", 10);
            dgvT_F2.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvT_F2.AddColumn("岗位工程名称", "WorkName", 35);
            dgvT_F2.AddColumn("信号源", "SignalMark", 20);

            dgvDR.AutoGenerateColumns = false;
            dgvDR.AllowUserToAddRows = false;
            dgvDR.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvDR.ReadOnly = true;
            dgvDR.AddColumn("序号", "ID", 10);
            dgvDR.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvDR.AddColumn("岗位工程名称", "WorkName", 35);
            dgvDR.AddColumn("信号源", "SignalMark", 20);

            dgvT_F4.AutoGenerateColumns = false;
            dgvT_F4.AllowUserToAddRows = false;
            dgvT_F4.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvT_F4.ReadOnly = true;
            dgvT_F4.AddColumn("序号", "ID", 10);
            dgvT_F4.AddColumn("岗位输入地址", "WorkAddress", 35);
            dgvT_F4.AddColumn("岗位工程名称", "WorkName", 35);
            dgvT_F4.AddColumn("信号源", "SignalMark", 20);

            LoadDgv();
            LoadMusicSet();
            LoadBoradcastSet();
            LoadOther();
        }

        private void LoadDgv()
        {
            var list = new ProjectSetBLL().GetModelAll();
            dgvT_IP.DataSource = null;
            dgvC_EG.DataSource = null;
            dgvT_F1.DataSource = null;
            dgvDR.DataSource = null;
            dgvT_IP.DataSource = list.Where(s => s.AreaType == 1).ToList();
            dgvC_EG.DataSource = list.Where(s => s.AreaType == 2).ToList();
            dgvT_F1.DataSource = list.Where(s => s.AreaType == 3).ToList();
            dgvT_F2.DataSource = list.Where(s => s.AreaType == 4).ToList();
            dgvDR.DataSource = list.Where(s => s.AreaType == 5).ToList();
            dgvT_F4.DataSource = list.Where(s => s.AreaType == 6).ToList();
        }

        /// <summary>
        /// 加载音乐设置
        /// </summary>
        private void LoadMusicSet()
        {
            try
            {
                LogObj.Info("加载音乐设置");
                musicSetList = musicSetBLL.GetModelAll();
                if (musicSetList != null && musicSetList.Count > 0)
                {
                    foreach (var item in musicSetList)
                    {
                        if (item.ID == 1)
                        {
                            this.intMusicNo_T_IP.Value = item.MusicId;
                            this.swMusicNF_T_IP.Active = (item.MusicState);
                            this.intMusictime_T_IP.Value = item.PlayTimes;
                            this.swMusicIsPlayAlong_T_IP.Active = (item.IsPlayAlong);
                        }
                        else if (item.ID == 2)
                        {
                            this.intMusicNo_C_EG.Value = item.MusicId;
                            this.swMusicNF_C_EG.Active = (item.MusicState);
                            this.intMusictime_C_EG.Value = item.PlayTimes;
                            this.swMusicIsPlayAlong_C_EG.Active = (item.IsPlayAlong);
                        }
                        else if (item.ID == 3)
                        {
                            this.intMusicNo_T_F1.Value = item.MusicId;
                            this.swMusicNF_T_F1.Active = (item.MusicState);
                            this.intMusictime_T_F1.Value = item.PlayTimes;
                            this.swMusicIsPlayAlong_T_F1.Active = (item.IsPlayAlong);
                        }
                        else if (item.ID == 4)
                        {
                            this.intMusicNo_T_F2.Value = item.MusicId;
                            this.swMusicNF_T_F2.Active = (item.MusicState);
                            this.intMusictime_T_F2.Value = item.PlayTimes;
                            this.swMusicIsPlayAlong_T_F2.Active = (item.IsPlayAlong);
                        }
                        else if (item.ID == 5)
                        {
                            this.intMusicNo_DR.Value = item.MusicId;
                            this.swMusicNF_DR.Active = (item.MusicState);
                            this.intMusictime_DR.Value = item.PlayTimes;
                            this.swMusicIsPlayAlong_DR.Active = (item.IsPlayAlong);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        private void LoadOther()
        {
            var sysSet = systemSetupBLL.GetModel();
            txtExceptionCall.Text = sysSet.ExceptionCall.ToString();
        }

        /// <summary>
        /// 广播状态
        /// </summary>
        /// <param name="linkState"></param>
        public void SetButtonState(Label labelState,UISymbolButton btnPlay, UISymbolButton btnPrep, UISymbolButton btnAdd, UISymbolButton btnStop, UITrackBar trackBar, string ip)
        {
            var linkState = GolbalVariable.VoiceSever.disSocClient.ContainsKey(ip);
            if (linkState)
            {
                labelState.Text = "在线";
                labelState.ForeColor = Color.Black;
                labelState.BackColor = Color.LimeGreen;
                btnPlay.Enabled = true;
                btnPrep.Enabled = true;
                btnAdd.Enabled = true;
                btnStop.Enabled = true;
                trackBar.Enabled = false;
            }
            else
            {
                labelState.Text = "离线";
                labelState.ForeColor = Color.Red;
                labelState.BackColor = Color.FromArgb(244, 244, 244);
                btnPlay.Enabled = false;
                btnPrep.Enabled = false;
                btnAdd.Enabled = false;
                btnStop.Enabled = false;
                trackBar.Enabled = false;
            }
        }

        /// <summary>
        /// 获取音乐设置
        /// </summary>
        private void GetMusicSet(ref bool isNew)
        {
            if (musicSetList != null && musicSetList.Count > 0)
            {
                isNew = false;
                foreach (var item in musicSetList)
                {
                    if (item.ID == 1)
                    {
                        item.MusicId = Convert.ToInt16(this.intMusicNo_T_IP.Value);
                        item.MusicState = this.swMusicNF_T_IP.Active;
                        item.PlayTimes = Convert.ToInt32(this.intMusictime_T_IP.Value);
                        item.IsPlayAlong = this.swMusicIsPlayAlong_T_IP.Active;
                    }
                    else if (item.ID == 2)
                    {
                        item.MusicId = Convert.ToInt16(this.intMusicNo_C_EG.Value);
                        item.MusicState = this.swMusicNF_C_EG.Active;
                        item.PlayTimes = Convert.ToInt32(this.intMusictime_C_EG.Value);
                        item.IsPlayAlong = this.swMusicIsPlayAlong_C_EG.Active;
                    }
                    else if (item.ID == 3)
                    {
                        item.MusicId = Convert.ToInt16(this.intMusicNo_T_F1.Value);
                        item.MusicState = this.swMusicNF_T_F1.Active;
                        item.PlayTimes = Convert.ToInt32(this.intMusictime_T_F1.Value);
                        item.IsPlayAlong = this.swMusicIsPlayAlong_T_F1.Active;
                    }
                    else if (item.ID == 4)
                    {
                        item.MusicId = Convert.ToInt16(this.intMusicNo_T_F2.Value);
                        item.MusicState = this.swMusicNF_T_F2.Active;
                        item.PlayTimes = Convert.ToInt32(this.intMusictime_T_F2.Value);
                        item.IsPlayAlong = this.swMusicIsPlayAlong_T_F2.Active;
                    }
                    else if (item.ID == 5)
                    {
                        item.MusicId = Convert.ToInt16(this.intMusicNo_DR.Value);
                        item.MusicState = this.swMusicNF_DR.Active;
                        item.PlayTimes = Convert.ToInt32(this.intMusictime_DR.Value);
                        item.IsPlayAlong = this.swMusicIsPlayAlong_DR.Active;
                    }
                }
            }
            else
            {
                isNew = true;
                musicSetList = new List<MusicSetModel>();
                MusicSetModel item = new MusicSetModel();
                item.ID = 1;
                item.MusicId = Convert.ToInt16(this.intMusicNo_T_IP.Value);
                item.MusicState = this.swMusicNF_T_IP.Active;
                item.PlayTimes = Convert.ToInt32(this.intMusictime_T_IP.Value);
                item.IsPlayAlong = this.swMusicIsPlayAlong_T_IP.Active;
                item.AreaName = "IP-SPS";
                item.AreaType = 1;
                musicSetList.Add(item);
                //
                item = new MusicSetModel();
                item.ID = 2;
                item.MusicId = Convert.ToInt16(this.intMusicNo_C_EG.Value);
                item.MusicState = this.swMusicNF_C_EG.Active;
                item.PlayTimes = Convert.ToInt32(this.intMusictime_C_EG.Value);
                item.IsPlayAlong = this.swMusicIsPlayAlong_C_EG.Active;
                item.AreaName = "C-SPS";
                item.AreaType = 2;
                musicSetList.Add(item);
                //
                item = new MusicSetModel();
                item.ID = 3;
                item.MusicId = Convert.ToInt16(this.intMusicNo_T_F1.Value);
                item.MusicState = this.swMusicNF_T_F1.Active;
                item.PlayTimes = Convert.ToInt32(this.intMusictime_T_F1.Value);
                item.IsPlayAlong = this.swMusicIsPlayAlong_T_F1.Active;
                item.AreaName = "F-SPS-1";
                item.AreaType = 3;
                musicSetList.Add(item);
                //
                item = new MusicSetModel();
                item.ID = 4;
                item.MusicId = Convert.ToInt16(this.intMusicNo_T_F2.Value);
                item.MusicState = this.swMusicNF_T_F2.Active;
                item.PlayTimes = Convert.ToInt32(this.intMusictime_T_F2.Value);
                item.IsPlayAlong = this.swMusicIsPlayAlong_T_F2.Active;
                item.AreaName = "F-SPS-2";
                item.AreaType = 4;
                musicSetList.Add(item);
                //
                item = new MusicSetModel();
                item.ID = 5;
                item.MusicId = Convert.ToInt16(this.intMusicNo_DR.Value);
                item.MusicState = this.swMusicNF_DR.Active;
                item.PlayTimes = Convert.ToInt32(this.intMusictime_DR.Value);
                item.IsPlayAlong = this.swMusicIsPlayAlong_DR.Active;
                item.AreaName = "DR-SPS";
                item.AreaType = 5;
                musicSetList.Add(item);
            }
        }
        /// <summary>
        /// 加载广播配置
        /// </summary>
        private void LoadBoradcastSet()
        {
            try
            {
                broadcastSetList = broadcastSetBLL.GetModelAll();
                LogObj.Info("广播配置数量："+broadcastSetList.Count);
                if (broadcastSetList != null && broadcastSetList.Count > 0)
                {
                    foreach (var item in broadcastSetList)
                    {
                        if (item.ID == 1)
                        {
                            this.panelSN1.Text = item.ID.ToString();
                            this.txtIP1.Text = item.IPAddress;
                            this.swBroadcastNF1.Active = item.BroadcastState;
                            this.trackBarVolume_1.Value = item.BroadcastVolume;
                            SetButtonState(labelState_1, btnPlay_1, btnPrep_1, btnAdd_1, btnStop_1,trackBarVolume_1, item.IPAddress);
                        }
                        else if (item.ID == 2)
                        {
                            this.panelSN2.Text = item.ID.ToString();
                            this.txtIP2.Text = item.IPAddress;
                            this.swBroadcastNF2.Active = item.BroadcastState;
                            this.trackBarVolume_2.Value = item.BroadcastVolume;
                            SetButtonState(labelState_2, btnPlay_2, btnPrep_2, btnAdd_2, btnStop_2, trackBarVolume_2, item.IPAddress);
                        }
                        else if (item.ID == 3)
                        {
                            this.panelSN3.Text = item.ID.ToString();
                            this.txtIP3.Text = item.IPAddress;
                            this.swBroadcastNF3.Active = item.BroadcastState;
                            this.trackBarVolume_3.Value = item.BroadcastVolume;
                            SetButtonState(labelState_3, btnPlay_3, btnPrep_3, btnAdd_3, btnStop_3, trackBarVolume_3, item.IPAddress);
                        }
                        else if (item.ID == 4)
                        {
                            this.panelSN4.Text = item.ID.ToString();
                            this.txtIP4.Text = item.IPAddress;
                            this.swBroadcastNF4.Active = item.BroadcastState;
                            this.trackBarVolume_4.Value = item.BroadcastVolume;
                            SetButtonState(labelState_4, btnPlay_4, btnPrep_4, btnAdd_4, btnStop_4, trackBarVolume_4, item.IPAddress);
                        }
                        else if (item.ID == 5)
                        {
                            this.panelSN5.Text = item.ID.ToString();
                            this.txtIP5.Text = item.IPAddress;
                            this.swBroadcastNF5.Active = item.BroadcastState;
                            this.trackBarVolume_5.Value = item.BroadcastVolume;
                            SetButtonState(labelState_5, btnPlay_5, btnPrep_5, btnAdd_5, btnStop_5, trackBarVolume_5, item.IPAddress);
                        }
                        else if (item.ID == 6)
                        {
                            this.panelSN6.Text = item.ID.ToString();
                            this.txtIP6.Text = item.IPAddress;
                            this.swBroadcastNF6.Active = item.BroadcastState;
                            this.trackBarVolume_6.Value = item.BroadcastVolume;
                            SetButtonState(labelState_6, btnPlay_6, btnPrep_6, btnAdd_6, btnStop_6, trackBarVolume_6, item.IPAddress);
                        }
                        else if (item.ID == 7)
                        {
                            this.panelSN7.Text = item.ID.ToString();
                            this.txtIP7.Text = item.IPAddress;
                            this.swBroadcastNF7.Active = item.BroadcastState;
                            this.trackBarVolume_7.Value = item.BroadcastVolume;
                            SetButtonState(labelState_7, btnPlay_7, btnPrep_7, btnAdd_7, btnStop_7, trackBarVolume_7, item.IPAddress);
                        }
                        else if (item.ID == 8)
                        {
                            this.panelSN8.Text = item.ID.ToString();
                            this.txtIP8.Text = item.IPAddress;
                            this.swBroadcastNF8.Active = item.BroadcastState;
                            this.trackBarVolume_8.Value = item.BroadcastVolume;
                            SetButtonState(labelState_8, btnPlay_8, btnPrep_8, btnAdd_8, btnStop_8, trackBarVolume_8, item.IPAddress);
                        }
                        else if (item.ID == 9)
                        {
                            this.panelSN9.Text = item.ID.ToString();
                            this.txtIP9.Text = item.IPAddress;
                            this.swBroadcastNF9.Active = item.BroadcastState;
                            this.trackBarVolume_9.Value = item.BroadcastVolume;
                            SetButtonState(labelState_9, btnPlay_9, btnPrep_9, btnAdd_9, btnStop_9, trackBarVolume_9, item.IPAddress);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogObj.Error(ex.Message);
            }
        }

        /// <summary>
        /// 获取广播配置
        /// </summary>
        private void GetBoradcastSet(ref bool isNew)
        {
            if (broadcastSetList.Count != 9)
                broadcastSetList.Clear();
            if (broadcastSetList != null && broadcastSetList.Count > 0)
            {
                isNew = false;
                foreach (var item in broadcastSetList)
                {
                    if (item.ID == 1)
                    {
                        item.ID = Convert.ToInt16(this.panelSN1.Text);
                        item.IPAddress = this.txtIP1.Text;
                        item.BroadcastState = this.swBroadcastNF1.Active;
                        item.BroadcastVolume = this.trackBarVolume_1.Value;
                    }
                    else if (item.ID == 2)
                    {
                        item.ID = Convert.ToInt16(this.panelSN2.Text);
                        item.IPAddress = this.txtIP2.Text;
                        item.BroadcastState = this.swBroadcastNF2.Active;
                        item.BroadcastVolume = this.trackBarVolume_2.Value;
                    }
                    else if (item.ID == 3)
                    {
                        item.ID = Convert.ToInt16(this.panelSN3.Text);
                        item.IPAddress = this.txtIP3.Text;
                        item.BroadcastState = this.swBroadcastNF3.Active;
                        item.BroadcastVolume = this.trackBarVolume_3.Value;
                    }
                    else if (item.ID == 4)
                    {
                        item.ID = Convert.ToInt16(this.panelSN4.Text);
                        item.IPAddress = this.txtIP4.Text;
                        item.BroadcastState = this.swBroadcastNF4.Active;
                        item.BroadcastVolume = this.trackBarVolume_4.Value;
                    }
                    else if (item.ID == 5)
                    {
                        item.ID = Convert.ToInt16(this.panelSN5.Text);
                        item.IPAddress = this.txtIP5.Text;
                        item.BroadcastState = this.swBroadcastNF5.Active;
                        item.BroadcastVolume = this.trackBarVolume_5.Value;
                    }
                    else if (item.ID == 6)
                    {
                        item.ID = Convert.ToInt16(this.panelSN6.Text);
                        item.IPAddress = this.txtIP6.Text;
                        item.BroadcastState = this.swBroadcastNF6.Active;
                        item.BroadcastVolume = this.trackBarVolume_6.Value;
                    }
                    else if (item.ID == 7)
                    {
                        item.ID = Convert.ToInt16(this.panelSN7.Text);
                        item.IPAddress = this.txtIP7.Text;
                        item.BroadcastState = this.swBroadcastNF7.Active;
                        item.BroadcastVolume = this.trackBarVolume_7.Value;
                    }
                    else if (item.ID == 8)
                    {
                        item.ID = Convert.ToInt16(this.panelSN8.Text);
                        item.IPAddress = this.txtIP8.Text;
                        item.BroadcastState = this.swBroadcastNF8.Active;
                        item.BroadcastVolume = this.trackBarVolume_8.Value;
                    }
                    else if (item.ID == 9)
                    {
                        item.ID = Convert.ToInt16(this.panelSN9.Text);
                        item.IPAddress = this.txtIP9.Text;
                        item.BroadcastState = this.swBroadcastNF9.Active;
                        item.BroadcastVolume = this.trackBarVolume_9.Value;
                    }
                }
            }
            else
            {
                isNew = true;
                broadcastSetList = new List<BroadcastSetModel>();
                BroadcastSetModel item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN1.Text);
                item.IPAddress = this.txtIP1.Text;
                item.BroadcastState = this.swBroadcastNF1.Active;
                item.AreaType = 1;
                item.AreaName = "IP-SPS";
                broadcastSetList.Add(item);
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN2.Text);
                item.IPAddress = this.txtIP2.Text;
                item.BroadcastState = this.swBroadcastNF2.Active;
                item.AreaType = 1;
                item.AreaName = "IP-SPS";
                broadcastSetList.Add(item);
                //
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN3.Text);
                item.IPAddress = this.txtIP3.Text;
                item.BroadcastState = this.swBroadcastNF3.Active;
                item.AreaType = 2;
                item.AreaName = "C-SPS";
                broadcastSetList.Add(item);
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN4.Text);
                item.IPAddress = this.txtIP4.Text;
                item.BroadcastState = this.swBroadcastNF4.Active;
                item.AreaType = 2;
                item.AreaName = "C-SPS";
                broadcastSetList.Add(item);
                //
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN5.Text);
                item.IPAddress = this.txtIP5.Text;
                item.BroadcastState = this.swBroadcastNF5.Active;
                item.AreaType = 3;
                item.AreaName = "F-SPS-1";
                broadcastSetList.Add(item);
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN6.Text);
                item.IPAddress = this.txtIP6.Text;
                item.BroadcastState = this.swBroadcastNF6.Active;
                item.AreaType = 4;
                item.AreaName = "F-SPS-2";
                broadcastSetList.Add(item);
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN7.Text);
                item.IPAddress = this.txtIP7.Text;
                item.BroadcastState = this.swBroadcastNF7.Active;
                item.AreaType = 4;
                item.AreaName = "F-SPS-2";
                broadcastSetList.Add(item);
                //
                item = new BroadcastSetModel();
                item.ID = Convert.ToInt16(this.panelSN9.Text);
                item.IPAddress = this.txtIP9.Text;
                item.BroadcastState = this.swBroadcastNF9.Active;
                item.AreaType = 5;
                item.AreaName = "DR-SPS";
                broadcastSetList.Add(item);
            }
            //去空IP
            broadcastSetList = broadcastSetList.Where(x => !string.IsNullOrEmpty(x.IPAddress)).ToList();
        }

        private void btnMusicSetSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            bool isNew = false;
            GetMusicSet(ref isNew);
            if (isNew)
            {
                if(musicSetBLL.AddList(musicSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            else
            {
                if (musicSetBLL.UpdateList(musicSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            //更新音乐参数缓存
            GolbalVariable.VoiceAreaMusicList = musicSetBLL.GetMusicBroadcastSet();
        }

        private void btnBroadcastSetSave_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            bool isNew = false;
            GetBoradcastSet(ref isNew);
            if (isNew)
            {
                if (broadcastSetBLL.AddList(broadcastSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            else
            {
                if (broadcastSetBLL.UpdateList(broadcastSetList).IsSuccess)
                    UIMessageTip.ShowOk("保存成功");
                else
                    UIMessageBox.ShowError("保存失败");
            }
            //更新音乐参数缓存
            GolbalVariable.VoiceAreaMusicList = musicSetBLL.GetMusicBroadcastSet();
        }

        private void dataGridView_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex >= 0)
                {
                    dataGridView = (UIDataGridView)sender;
                    //若行已是选中状态就不再进行设置
                    if (dataGridView.Rows[e.RowIndex].Selected == false)
                    {
                        dataGridView.ClearSelection();
                        dataGridView.Rows[e.RowIndex].Selected = true;
                    }
                    //只选中一行时设置活动单元格
                    if (dataGridView.SelectedRows.Count == 1)
                    {
                        dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    }
                    //弹出操作菜单
                    uiContextMenuStrip1.Show(MousePosition.X, MousePosition.Y);
                }
            }
        }

        /// <summary>
        /// 开捆设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            dataGridView = (UIDataGridView)sender;
            ProjectEdit(dataGridView.Name, dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
        }

        private void toolStripMenuItemUpdata_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ProjectEdit(dataGridView.Name, dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
        }

        private void toolStripMenuItemAdd_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ProjectEdit(dataGridView.Name);
        }

        private void toolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            if (UIMessageDialog.ShowMessageDialog("您确定要删除该工程吗？", UILocalize.AskTitle, true, UIStyle.Red, true, Frame?.TopMost ?? false))
            {
                ProjectDelect(dataGridView.CurrentRow.DataBoundItem as ProjectSetModel);
            }
        }

        /// <summary>
        /// 工程编辑
        /// </summary>
        /// <param name="model">工程模型</param>
        private void ProjectEdit(string dgvName, ProjectSetModel model = null)
        {
            var triaGroups = GolbalVariable.TriaGroupList.Where(x => x.SignalMark == "BUTTON").Select(x => x.ID).ToArray();
            var TriaItemList = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).ToList();
            var TriaItems = GolbalVariable.TriaItemList.Where(s => triaGroups.Contains(s.GroupID)).Select(x => x.SignalMark).ToArray();

            UIEditOption option = new UIEditOption();
            option.AutoLabelWidth = true;

            if (model != null)
            {
                option.Text = "工程设置(" + model.WorkName + ")";
                option.AddText("WorkAddress", "岗位输入地址", model.WorkAddress, true);
                option.AddText("WorkName", "岗位工程名称", model.WorkName, false);
                option.AddCombobox("SignalMark", "信号源标识", TriaItemList, "SignalMark", "SignalMark", model.SignalMark);
            }
            else
            {
                option.Text = "新增工程";
                option.AddText("WorkAddress", "岗位输入地址", null, true);
                option.AddText("WorkName", "岗位工程名称", null, false);
                option.AddCombobox("SignalMark", "信号源标识", TriaItems);
            }

            UIEditForm frm = new UIEditForm(option);
            frm.ShowDialog();

            if (frm.IsOK)
            {
                if (model != null)
                {
                    model.WorkAddress = frm["WorkAddress"].ToString();
                    model.WorkName = frm["WorkName"].ToString();
                    if (frm["SignalMark"] != null)
                        model.SignalMark = frm["SignalMark"].ToString();
                    if (projectSetBLL.Update(model).IsSuccess)
                        UIMessageTip.ShowOk("设置成功");
                    else
                        UIMessageBox.ShowError("设置失败！");
                    LogObj.Info("工程设置(" + model.WorkName + ")");
                }
                else
                {
                    model = new ProjectSetModel();
                    switch (dgvName)
                    {
                        case "dgvT_IP":
                            model.AreaType = 1;
                            model.AreaName = "IP-SPS";
                            break;
                        case "dgvC_EG":
                            model.AreaType = 2;
                            model.AreaName = "C-SPS";
                            break;
                        case "dgvT_F1":
                            model.AreaType = 3;
                            model.AreaName = "F-SPS-1";
                            break;
                        case "dgvT_F2":
                            model.AreaType = 4;
                            model.AreaName = "F-SPS-2";
                            break;
                        case "dgvDR":
                            model.AreaType = 5;
                            model.AreaName = "DR-SPS";
                            break;
                        case "dgvT_F4":
                            model.AreaType = 6;
                            model.AreaName = "F-SPS-4";
                            break;
                    }
                    model.WorkAddress = frm["WorkAddress"].ToString();
                    model.WorkName = frm["WorkName"].ToString();
                    model.SignalMark = TriaItems[int.Parse(frm["SignalMark"].ToString())];
                    if (projectSetBLL.Add(model).IsSuccess)
                        UIMessageTip.ShowOk("新增成功");
                    else
                        UIMessageBox.ShowError("新增失败！");
                    LogObj.Info("新增工程(" + model.WorkName + ")");
                }
                LoadDgv();
            }
            frm.Dispose();
        }

        /// <summary>
        /// 工程删除
        /// </summary>
        /// <param name="entityMaterialDirection"></param>
        private void ProjectDelect(ProjectSetModel model)
        {
            if (projectSetBLL.Delete(model).IsSuccess)
                UIMessageTip.ShowOk("删除成功");
            else
                UIMessageBox.ShowError("删除失败！");
            LoadDgv();
        }

        private void btnPlay_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            var item = broadcastSetList.Where(x=>x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            if (item == null)
                return;
            GolbalVariable.VoiceSever.ExecVoiceCommand(item.AreaType, true, item.IPAddress);
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            if (item == null)
                return;
            GolbalVariable.VoiceSever.ExecVoiceCommand(item.AreaType, false, item.IPAddress);
        }

        private void btnPrep_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            btn.Enabled = false;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            if (item == null)
                return;
            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(item.AreaType, false, item.IPAddress);
            var control = this.tabPageAreaBroadcast.Controls.Find("trackBarVolume_"+ int.Parse(btn.Name.Split('_')[1]), true);
            if (control != null && control.Length > 0)
            {
                UITrackBar trackBar = control[0] as UITrackBar;
                trackBar.Value--;
            }
            Thread.Sleep(50);
            btn.Enabled = true;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            UISymbolButton btn = (UISymbolButton)sender;
            btn.Enabled = false;
            var item = broadcastSetList.Where(x => x.ID == int.Parse(btn.Name.Split('_')[1])).FirstOrDefault();
            if (item == null)
                return;
            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(item.AreaType, true, item.IPAddress);
            var control = this.tabPageAreaBroadcast.Controls.Find("trackBarVolume_" + int.Parse(btn.Name.Split('_')[1]), true);
            if (control != null && control.Length > 0)
            {
                UITrackBar trackBar = control[0] as UITrackBar;
                trackBar.Value++;
            }
            Thread.Sleep(20);
            btn.Enabled = true;
        }

        private void btnVolumeCalibration_Click(object sender, EventArgs e)
        {
            if (ApplicationCache.CurrentUser.Permission > 0)
            {
                UIMessageBox.Show("该操作需要管理员权限，请先切换管理员账号！");
                return;
            }
            ShowWaitForm("正在校正音量，预计需要几十秒，请耐心等候...");         
            foreach (var item in broadcastSetList)
            {
                var linkState = GolbalVariable.VoiceSever.disSocClient.ContainsKey(item.IPAddress);
                if (linkState)
                {
                    var control = this.tabPageAreaBroadcast.Controls.Find("trackBarVolume_" + item.ID, true);
                    if (control != null && control.Length > 0)
                    {
                        UITrackBar trackBar = control[0] as UITrackBar;
                        for (int i = 0; i < 30; i++)
                        {
                            var broadcastSet = broadcastSetList.Where(x => x.ID == item.ID).FirstOrDefault();
                            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(broadcastSet.AreaType, false, broadcastSet.IPAddress);
                            Thread.Sleep(80);
                        }
                        for (int i = 0; i < trackBar.Value; i++)
                        {
                            var broadcastSet = broadcastSetList.Where(x => x.ID == item.ID).FirstOrDefault();
                            GolbalVariable.VoiceSever.ExecControlVoiceVolumeCommand(broadcastSet.AreaType, true, broadcastSet.IPAddress);
                            Thread.Sleep(80);
                        }
                    }
                }
                else
                {

                }
            }
            HideWaitForm();
        }

        private void intMusicNo_DR_ValueChanged(object sender, int value)
        {

        }

        private void btnSaveOther_Click(object sender, EventArgs e)
        {
            var sysSet = systemSetupBLL.GetModel();
            sysSet.ExceptionCall= int.Parse(txtExceptionCall.Text);
            var result = systemSetupBLL.Update(sysSet);
            if(result.IsSuccess)
                UIMessageTip.ShowOk("保存成功");
            else
            {
                LogObj.Error("保存失败(" + result.Dsca + ")");
                UIMessageBox.ShowError("保存失败！");
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            LoadBoradcastSet();
        }
    }
}
